<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_GroupLoader
{
    var $Controller;
    var $Group;
    var $Request;
    function News_Command_GroupLoader($controller)
	{
	    $this->Controller = $controller;
        $this->Group      = $this->Controller->Settings['group'];
        $this->Request    = $this->Controller->Settings['request'];
        $this->Path       = "notify/{$this->Group}.group";
	}
	
	function GetURI()
	{
		$uri = "/ui/commandhandler/request/page/group/{$this->Group}/page/1";
		
		if (isset($this->Controller->Settings['response']))
		{
		    $uri .= '/response/xsl';	
		}
		
	    return array("RequestURI"=>"/ui/commandhandler/request/{$this->Request}/group/{$this->Group}/status/ok",
	                 "RedirectURI"=>$uri);
	}
	
	function GetStatus()
	{
	    if (file_exists("data/{$this->Group}.xml"))
        {
            return 'OK';
        } 
        return '';
	}
	
	function Invoke($command)
	{  
	    $this->Path = $command['path']; 
        $groupname  = $this->Controller->Read($command['path']);  
        $startkey   = NULL;
        $count      = DOWNLOAD_PACKET_VOLUME;
        unlink ($this->Path);
        
        if (strpos($groupname, '|')!==false)
        {
            $array = explode ('|', $groupname);
            $groupname = $array[0];
            $startkey  = $array[1];
            if (isset($array[2])) $count = $array[2];
        }
        
        $filename  = "data/{$groupname}.xml";
		$this->Controller->LogEntry ("Loading group {$groupname}", 1, 1);
        if (file_exists($filename)) 
        {
            return;
        }
        $this->Controller->Settings['group'] = $groupname;
        $this->Controller->Server = new News_Usenet_Server($this->Controller);
        
        $xml = $this->Controller->Server->LoadNewsGroup($count, $startkey);
		
        // $list = new News_UI_Articlelist($this->Controller, false);
//         
        // $list->Load($count, $startkey); 
        // $xml = $list->Display(true);
		
        $this->Controller->Save ($filename, $xml);    
	}
	
	function Signal()
	{ 
        $count   = $this->Controller->Settings['count']; 
        $more    = $this->Controller->Settings['more']; 
        $status  = $this->Controller->Settings['status']; 
        $message = $this->Group;
        
        if (strlen($status) > 0)
        {
            return $this->GetStatus ();
        }
        
        if ($this->Request == 'renew')
        {
            $oldname = "data/{$this->Group}.xml";
            if (strlen($more) > 0)
            {
                for ($fn=1;$fn<100;$fn++)
                {
                     $newname = "data/{$this->Group},{$fn}.xml";
                     if (!file_exists($newname))
                     {
                         rename ($oldname, $newname);
                         break;
                     }
                 } 
                 $message .= "|{$more}";
                 if (strlen($count) > 0)
                 {
                     $message .= "|{$count}";
                 }
             }
             else
             {
                unlink ($oldname); 
                for ($fn=1;$fn<100;$fn++)
                {
                     $newname = "data/{$this->Group},{$fn}.xml";
                     if (file_exists($newname))
                     {
                         unlink ($newname); 
                     } else break;
                 } 
             
             }
        } 
        
        if ($this->Request == 'restore')
        {
             $newname = "data/{$this->Group},{$more}.xml";  
             $oldname = "data/{$this->Group}.xml";
             if (file_exists($newname))
             {
                  rename ($oldname, $oldname.'.tmp');
                  rename ($newname, $oldname);
                  rename ($oldname.'.tmp', $newname);
                  redirect ("/ui/articlelist/group/{$this->Group}/page/1");
             }
        } 
        
        $this->Controller->Save($this->Path, $message); 
        return $this->Controller->Dialog ($this->GetURI());
	}
}



